package com.my.cd.builder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 假期计划综合类（这个就是生成器要生成的主体对象）
 */
public class Vacation {

    private List<VacationDay> vacationDays;//假期行程列表
    private Date startDate;//起始日期
    private int days = 0;//假期总天数
    private VacationDay vacationDay;//当前行程计划

    public Vacation(String std) {
        vacationDays = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            startDate = sdf.parse(std);
            vacationDay = new VacationDay(startDate);
            vacationDays.add(vacationDay);
            days++;
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public void setStartDate(String std) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            startDate = sdf.parse(std);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public Date getStartDate() {
        return startDate;
    }

    public void addDay() {
        vacationDay = new VacationDay(nextDate(days));
        vacationDays.add(vacationDay);
        days++;
    }

    /**
     * 设置假期计划当前从第几天开始（用来跳过前面几天）
     */
    public boolean setVacationDay(int i) {
        if (i > 0 && i < vacationDays.size()) {
            vacationDay = vacationDays.get(i);
            return true;
        }
        vacationDay = null;
        return false;
    }

    public void setHotel(String mHotels) {
        vacationDay.setHotel(mHotels);
    }

    public void addTicket(String ticket) {
        vacationDay.addTicket(ticket);
    }

    public void addEvent(String event) {
        vacationDay.addEvent(event);
    }

    public void showInfo() {
        for (int i = 0, len = vacationDays.size(); i < len; i++) {
            System.out.println("** " + (i + 1) + "/" + len + " day**");
            System.out.println(vacationDays.get(i).showInfo());

        }
    }

    private Date nextDate(int n) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        cal.add(Calendar.DATE, n);
        return cal.getTime();
    }
}
